package com.lsjenglish.englishstudy.controller;

import com.lsjenglish.englishstudy.domain.ShowEnglishWorldEntity;
import com.lsjenglish.englishstudy.domain.ShowWorldEntity;
import com.lsjenglish.englishstudy.service.IShowEnglishWorldService;
import com.lsjenglish.englishstudy.service.IShowWorldService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.List;

/**
 * @author Lsj
 * @version 1.0
 * @date 2021/7/19 19:47
 */
@Controller
@AllArgsConstructor
public class ShowEnglishWorld {

    private final IShowEnglishWorldService showEnglishWorldService;

    private final IShowWorldService showWorldService;

    @RequestMapping("/")
    public String showEnglish(Model model) throws IOException {
        // 用户
        List<ShowEnglishWorldEntity> list = showEnglishWorldService.englishWorld();
        list.forEach(System.out::println);
        model.addAttribute("list", list);
        // 每次显示的单词是等级的个数*5
        List<ShowWorldEntity> list1 = showWorldService.englishWorldS();
        // 单词数量大于等级的情况
        if ((list1.size()/5) > Integer.parseInt(list.get(0).getSwordLevel())) {
            List<ShowWorldEntity> list2 = list1.subList(0, Integer.parseInt(list.get(0).getSwordLevel())*5);
            // 明天试一下接口
            model.addAttribute("list1", list2);
        } else {
            model.addAttribute("list1", list1);
        }
        return "/ShowEnglishWorld";
    }

    @RequestMapping("/addSword")
    @ResponseBody
    public List<ShowEnglishWorldEntity> showWorldEntity() throws IOException {
        List<ShowEnglishWorldEntity> list = showEnglishWorldService.englishWorld();
        // 进行计算,每次升级需要经验是当前等级的五倍 当经验值满了之后 等级加一 战斗力加5
        Long levelNeed = list.get(0).getSwordLevelCache();
        // 每次点击经验+1 也就是所需经验-1
        list.get(0).setSwordLevelCache(levelNeed - 1);
        // 当前等级
        int level = Integer.parseInt(list.get(0).getSwordLevel());
        // 战力
        int power = list.get(0).getSwordNum();
        if (levelNeed <= 1) {
            list.get(0).setSwordLevel(level+1+"");
            list.get(0).setSwordNum(power+5);
            list.get(0).setSwordLevelCache((level + 1) * 5L);
        }
        // 修改数据库数据
        showEnglishWorldService.updateSword(list.get(0));
        return list;
    }
}
